<?php

/*
 * Controller/VariableController.php
 */

namespace RHX\Controller;

use Zend\View\Model\JsonModel;
use RHX\Model\Object\XCalculator;
use RHX\Model\Table\PlanningSemaineTable;

class VariableController extends RHXController {

    public function indexAction() {
	// Changement de Layout
	$this->layout('layout/rhx2');

	// tables
	$siteTable	 = $this->getService('Table\SiteTable');
	$periodeTable	 = $this->getService('Table\PeriodeTable');

	// ID du site
	$id = $this->params()
		->fromRoute('id', -1);

	// ID de la période
	$periodeId = $this->params()
		->fromRoute('other1', -1);

	// Site
	$site = null;
	if ($id < 0) {
	    $site = $this->navigation->offsetGet('site');
	}
	else {
	    $site = $siteTable->get($id);
	    $this->changeSite($id);
	}

	// Periode
	$periode = null;
	if ($periodeId < 0) {
	    $periode = $this->navigation->offsetGet('periode');
	}
	else {
	    $periode = $periodeTable->get($periodeId);
	    $this->changePeriode($periodeId);
	}


	return array(
	    'site'		 => $site,
	    'periode'	 => $periode,
	);
    }

    public function loadIndexAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Table
	$planningSemaineTable	 = $this->getService('Table\PlanningSemaineTable');
	$employeTable		 = $this->getService('Table\EmployeTable');

	// Données en POST
	$post = $this->getRequest()
		->getPost();

	// Variables
	$site	 = $post->get('site');
	$periode = $post->get('periode');

	// Assignation
	$this->changeSite($site);
	$this->changePeriode($periode);

	// Variables de paie
	$variables = $planningSemaineTable->getVariables($periode, $site);

	// traitement des données
	$employes	 = array();
	$lastMatricule	 = null;
	foreach ($variables as $var) {
	    $data = array();
	    if ($lastMatricule == $var['matricule']) {
		// Mise à jour
		$data = $employes[$lastMatricule];
		//echo "<pre>UPDATE</pre>";
	    }
	    else {
		// Nouveau
		//echo "<pre>NEW</pre>";
		$lastMatricule		 = $var['matricule'];
		$data['matricule']	 = $var['matricule'];
		$data['nom']		 = $var['nom1'];
		$data['prenom']		 = $var['prenom'];
		$data['contrat_fin']	 = $var['contrat_fin'];
		$data['contrat_debut']	 = $var['contrat_debut'];
		$data['code']		 = $var['code'];
		$data['periode']	 = $var['periode_libelle'];
		$data['hebdo']		 = $var['hebdo'];

		$data['retard']		 = $var['retard'];
		$data['retard_heure']	 = $var['retard_heure'];
		$data['semaines']	 = array();
	    }
	    $sem				 = array();
	    $tmp				 = sscanf($var['semaine_libelle'], "Semaine %d");
	    //var_dump($tmp);
	    $semaineLibelle			 = "S" . $tmp[0];
	    $sem['libelle']			 = $semaineLibelle;
	    $sem['lundi']			 = $var['lundi'];
	    $sem['dimanche']		 = $var['dimanche'];
	    $sem['heure_travaille']		 = floatval($var['heure_travaille']);
	    $sem['heure_non_plannifie']	 = floatval($var['heure_non_plannifie']);
	    $sem['hebdo']			 = floatval($var['hebdo']);
	    $sem['taux']			 = floatval($var['taux']);
	    $sem['retard']			 = (int) $var['retard'];
	    $sem['retard_heure']		 = $var['retard_heure'];

	    $index				 = (int) $tmp[0];
	    $data['semaines'][--$index]	 = $sem;

	    $lastMatricule = $var['matricule'];

	    // Employé et son coefficient
	    $emp				 = $employeTable->get($lastMatricule);
	    $data['coef']			 = $emp->getCoefficient();
	    $employes[$lastMatricule]	 = $data;
	}

	// Liste des employes du site
	$employeList = $employeTable->getEmployesBySite($site);

	return array(
	    'post'		 => $post,
	    'variables'	 => $variables,
	    'employes'	 => $employes,
	    'employeList'	 => $employeList,
	    'adapter'	 => $this->getAdapter(),
	);
    }

    public function jsonPeriodeListAction() {
	// Table
	$periodeTable = $this->getService('Table\PeriodeTable');

	// Liste des périodes
	$periodeList	 = $periodeTable->getAll();
	$periodes	 = array();

	// Parcours de la liste des périodes
	foreach ($periodeList as $p) {
	    $periodes[$p->id] = $p->libelle;
	}

	return new JsonModel($periodes);
    }

    /**
     * Liste des semaines d'une période
     * 
     * @return \Zend\View\Model\JsonModel
     */
    public function getSemaineAction() {
	// Table Periode Semaine
	$periodeSemaineTable = $this->getService('Table\PeriodeSemaineTable');

	// POST
	$post = $this->getRequest()
		->getPost();

	// Semaines
	$semaineList = $periodeSemaineTable->getSemainesByPeriode($post->periode);

	// Parcours
	$semaines = array();
	foreach ($semaineList as $semaine) {
	    if ($semaine->libelle == 'Semaine ' . $post->semaine) {
		$semaines['semaine'] = $semaine;
		break;
	    }
	}

	return new JsonModel($semaines);
    }

    public function getTemplateAction() {
	// Vue Planning Semaine Periode
	$viewPS = $this->getService('View\PSPlanningView');

	// POST
	$post = $this->getRequest()
		->getPost();

	// PSPlanning
	$row = $viewPS->select(function(\Zend\Db\Sql\Select $select) use($post) {
			    $where = new \Zend\Db\Sql\Where();
			    $where->equalTo('semaine_id', $post->semaine);
			    $where->equalTo('site_id', $post->site);
			    $select->where($where);
			})->current();

	$template = array('template' => 0);

	if (!$row) {
	    $template['template'] = 0;
	}
	else {
	    $template['template'] = $row->template;
	}

	return new JsonModel($template);
    }
    
    /**
     * Chargement du contenu de la page d'index
     * 
     * @return array
     */
    public function loadAction(){
	$this->layout('layout/ajax');
	$post = $this->getRequest()
		->getPost();
	$manager = new \RHX\Model\Manager\VariableManager($this->getAdapter());
        $empManager = new \RHX\Model\Manager\EmployeManager($this->getAdapter());
        
        return array(
            'employe' => $empManager->getEmployesByPeriode($post->site, $post->periode, 'poste_rang asc, prenom asc'),
	    'periode' => $manager->getPeriode($post->periode),
	    'manager' => $manager,
	    'post' => $post,
	    'transferts' => $manager->orderTransfert($manager->getTransfertEntrant($post->periode, $post->site)),
            'planningSemTbl' =>  $this->getService('Table\PlanningSemaineTable'),
	);
    }
    
    /* action de test 
     */
    public function devHeuresModuleeAction(){
        //vbl
        $idPrd = 19 ; // periode du mois d' octobre
        
        //tbl
        $planningSemTbl = new PlanningSemaineTable($this->getAdapter()); 
        
        $manager = new \RHX\Model\Manager\VariableManager($this->getAdapter());
        $periode = $manager->getPeriode($idPrd);
        
        //ps
        print(' PS emp : 125 (site printemps) <br>');        
        $plannings = $planningSemTbl->getPlanningByPeriode($periode, $matricule = 125 );
                
        $calculator = new XCalculator($this->getAdapter());
        $totHrMod = $calculator->getSumHeuresModulees($plannings);
        
        var_dump($totHrMod);
        die;
    } 

}

?>
